import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-20
 * Time: 22:25
 */
//1027.最长等差数列
class Solution7 {
    public int longestArithSeqLength(int[] nums) {
        int n = nums.length;
        //优化
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(nums[0],0);

        //创建dp表
        int[][] dp = new int[n][n];
        //dp[i][i]以i结尾以及以j结尾的所有子序列最长等差数列长度

        //初始化
        for(int j = 0; j < n; j++) {
            for(int i = 0; i < j; i++) {
                dp[i][j] = 2;
            }
        }
        /*
        for(int i = 0; i < n; i++) {
            Arrays.fill(dp[i],2);
        }
        */
        int ret = 2;
        for(int i = 1; i < n; i++) { //固定倒数第二个数
            for(int j = i+1; j < n; j++) { //枚举倒数第一个数
                int a = (2*nums[i])-nums[j];
                if(hash.containsKey(a)) {
                    dp[i][j] = dp[hash.get(a)][i] + 1;
                    ret = Math.max(dp[i][j],ret);
                }
            }
            hash.put(nums[i],i);
        }
        return ret;
    }
}
